package com.hut.oa.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hut.oa.model.bean.CommentReply;
import com.hut.oa.model.bean.CommentThumbs;
import com.hut.oa.model.bean.DiscussComment;
import com.hut.oa.model.bean.User;
import com.hut.oa.model.dao.discuss.CommentThumbsDao;
import com.hut.oa.model.dao.discuss.DiscussCommentDao;
import com.hut.oa.model.dao.discuss.DiscussCommentReplyDao;
import com.hut.oa.model.dao.discuss.DiscussDao;

@Service
@Transactional(readOnly=true)
public class DiscussService {
	@Autowired
	AnnounceService as;
	
	@Autowired
	DiscussCommentDao dcdao;
	
	@Autowired
	DiscussDao ddao;
	
	@Autowired
	CommentThumbsDao ctdao;
	
	@Autowired
	DiscussCommentReplyDao dcrdao;
	
	//评论
	@Transactional
	public void updateCommentNum(DiscussComment comment){
		DiscussComment discussComment=dcdao.save(comment);
		discussComment.getDiscuss().setNewTime(new Date());
		dcdao.save(comment);
		ddao.updateCommentNum(comment.getDiscuss().getId());
		//发送通知
		Integer type=comment.getDiscuss().getType();
		String url="/discuss/view";
		if(type==2){
			url="/discuss/view/vote";
		}else if(type==3){
			url="/discuss/view/active";
		}
		List<User> users=new ArrayList<>();
		users.add(comment.getDiscuss().getUser());
		as.insertAnnounce(users, comment.getDiscuss().getStatus(), "你发布的帖子有新的评论！", url, comment.getDiscuss().getId(), comment.getUser());
	}
	
	//点赞
	@Transactional
	public void updatethumbNum(CommentThumbs thumbs){
		ctdao.save(thumbs);
		dcdao.updatethumbNum(thumbs.getComment().getId());
	}
	
	//回复
	@Transactional
	public void updatereplyNum(CommentReply commentReply){
		dcrdao.save(commentReply);
		dcdao.updatereplyNum(commentReply.getComment().getId());
		//发送通知
		Integer type=commentReply.getComment().getDiscuss().getType();
		String url="/discuss/view";
		if(type==2){
			url="/discuss/view/vote";
		}else if(type==3){
			url="/discuss/view/active";
		}
		List<User> users=new ArrayList<>();
		users.add(commentReply.getComment().getUser());
		as.insertAnnounce(users, commentReply.getComment().getDiscuss().getStatus(), "你的评论有新的回复！", url, commentReply.getComment().getDiscuss().getId(), commentReply.getUser());
	}
	
	
	
}
